package com.doit.day02;

import com.alibaba.fastjson.JSON;
import com.doit.day01.MyConfig;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.*;

/**
 * 1.每5s输出一次当前来了多少用户(去重)  uv
 * 2.将每条数据添加一个字段来标识，如果这个用户的id是第一次出现，那么就标注1，否则就是0
 */
public class _03_练习之消费者之布隆过滤器 {
    public static void main(String[] args) {
        //用布隆过滤器去判断
        //创建一个布隆过滤器对象
        BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), 1000000);

        Properties props = new Properties();
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, MyConfig.HOST_AND_PORT);
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG,"g01");
        //选配的
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"earliest");
        props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"false");
        //先将数据消费出来
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);
        consumer.subscribe(Arrays.asList("event-log"));
        while (true){
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(Long.MAX_VALUE));
            for (ConsumerRecord<String, String> consumerRecord : poll) {
                //value 数据拿出来 ==》 {}==> javabean对象 ==》 获取guid 放到set 集合中
                String json = consumerRecord.value();
                EventLog eventLog = JSON.parseObject(json, EventLog.class);
                int guid = eventLog.getGuid();
                //要去判断在还是不在
                boolean flag = bloomFilter.mightContain(guid);
                if (flag){
                    System.out.println("这是一个老用户"+json);
                }else {
                    System.out.println("这是一个新用户"+json);
                    //我就要将这个数据标注到布隆过滤器中
                    bloomFilter.put(guid);
                }
            }
        }

    }
}
